const Router = require('koa-router');
const Db = require("../service/TextDb");
const router = new Router({
    prefix: '/tc'
});

router.get('/', async (ctx, next) => {
    var params = ctx.request.query;
    var dbGroup = params.group ? params.group : 'default';
    Db.setDb(dbGroup);
    
    //获取数据列表
    var list = await Db.getData();

    await ctx.render('index', {
        list: list,
        group: dbGroup
    })
})

router.post('/save', async (ctx, next) => {
    var params = ctx.request.body;
    var dbGroup = params.group ? params.group : 'default';

    var d = new Date();
    var date = d.getFullYear().toString() + "-" + (d.getMonth() + 1).toString() + "-" + d.getDate();
    var time = d.getTime();

    await Db.setDb(dbGroup);
    //保存数据
    Db.push({
        date:date, 
        content: params.content,
        time: time
    });

    ctx.body = JSON.stringify('ok');
})


module.exports = router
